//
//  MobileInputVC.m
//  EPTool
//
//  Created by guoshiqing on 2018/4/13.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "MobileInputVC.h"
#import "SMSCodeInputVC.h"
#import "AccountView.h"

#import "Macro.h"
#import "EPColor.h"
#import "UIView+DDAddition.h"
#import "AccountView.h"
#import "EPButton.h"
#import "AccountSysVM.h"
#import "BaseWebVC.h"
#import "ErrorCode.h"
#import "UserSettings.h"

@interface MobileInputVC () <UITextFieldDelegate>
@property (nonatomic, copy) NSString *strAreaCode;
@property (nonatomic, copy) NSString *strMobile;

@property (nonatomic, strong) UILabel *lblTitle;
@property (nonatomic, strong) AreaCodeView *viwAreaCode;
@property (nonatomic, strong) AccountView *accountView;
@property (nonatomic, strong) EPButton *btnNext;
@property (nonatomic, strong) UIView *protocolView;
@property (nonatomic, strong) UIButton *btnProtocol;

@property (nonatomic, strong) UILabel *lblNote;
@property (nonatomic, strong) UILabel *lblMobile;

@end

@implementation MobileInputVC

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.lblMobile.text = [NSString stringWithFormat:@"%@+%@ %@", @"当前手机号：", self.strAreaCode, self.strMobile];
    if (ForForgotType == self.type) {
        self.lblTitle.text = @"输入手机号";
    }
    else if (ForRegisterType == self.type)
    {
        self.lblTitle.text = @"注册";
    }
    else if (ForModifyMobileType == self.type)
    {
        self.lblTitle.text = @"输入新号码";
    }
    else if ((ForBindingMobileTypeFromWX == self.type) || (ForBindingMobileTypeFromFB == self.type))
    {
        self.lblTitle.text = @"绑定手机号";
    }
    
    self.view.backgroundColor = [EPColor epColor_Bg];
    [self addSubviews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 接口调用返回结果处理
- (void)handelRequestData:(BaseJSONEntity *)returnValue
{
    [self hideProcessingHud];
    if (0 != returnValue.ret) {
        if (C_Login_PhoneIsCover == returnValue.ret) {
            WEAKSELF
            [self showAlertWithMessage:@"" title:returnValue.msg cancelStr:@"否" sureStr:@"是" cancel:^(UIAlertAction * _Nonnull action) {
            } sure:^(UIAlertAction * _Nonnull action) {
                [weakSelf getSMSWithThirdParty];
            }];
            return;
        }
        [self showAlertWithTitle:returnValue.msg];
        return;
    }
    if ([returnValue.url isEqualToString:RegisterCheckPhoneBindUrl]) {
        [self getSMSWithThirdParty];
        return;
    }
    
    [self goToSMSCodeInput];
}

- (void)goToSMSCodeInput
{
    SMSCodeInputVC *smsCodeVC = [[SMSCodeInputVC alloc] init];
    smsCodeVC.baseVM = [[AccountSysVM alloc] init];
    smsCodeVC.type = self.type;
    smsCodeVC.pwd = self.pwd;
    [smsCodeVC setAreaCode:self.strAreaCode andMobile:self.strMobile];
    [self.navigationController pushViewController:smsCodeVC animated:YES];
}

- (void)setAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile
{
    self.strAreaCode = areaCode;
    self.strMobile = mobile;
}

//#pragma mark - 添加控件
- (void)addSubviews
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.lblTitle];
    [self.view addSubview:self.lblNote];
    [self.view addSubview:self.lblMobile];
    [self.view addSubview:self.accountView];
    [self.view addSubview:self.btnNext];
    [self.view addSubview:self.protocolView];
    
    self.protocolView.hidden = ((ForRegisterType == self.type) || (ForBindingMobileTypeFromWX == self.type) || (ForBindingMobileTypeFromFB == self.type))?NO:YES;
    
    self.lblTitle.top = 16.0f;//(self.view.top == NavBar_Height)?16.f:(NavBar_Height+16.f);
    self.lblNote.top = self.lblTitle.bottom + 8.f;
    self.lblMobile.top = self.lblNote.bottom;
    self.accountView.top = CGRectGetMaxY(self.lblTitle.frame) + 62.f;
    self.btnNext.top = self.accountView.bottom + 20.f;
    self.protocolView.top = self.btnNext.bottom + Cell_X;
    
    WEAKSELF
    self.accountView.btnClearBlock = ^{
        weakSelf.btnNext.enabled = NO;
    };
    // 用户编辑状态UITextField长度变化
    self.accountView.txfBecomeBlock = ^(NSInteger length, NSString *mobile, NSString *areaCode) {
        if (0 < length) {
            weakSelf.btnNext.enabled = YES;
        }
        else // 通过光标选择范围的情况
        {
            weakSelf.btnNext.enabled = NO;
        }
    };
    
    self.lblNote.hidden = (ForModifyMobileType == self.type)?NO:YES;
    self.lblMobile.hidden = (ForModifyMobileType == self.type)?NO:YES;
}

- (UILabel *)lblTitle
{
    if (!_lblTitle) {
        _lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(LoginVC_X, 0, LoginVC_W, 32.f)];
        _lblTitle.textColor = [EPColor epColor_Blue];
        _lblTitle.font = SystemFont(23.f);
        _lblTitle.textAlignment = NSTextAlignmentLeft;
    }
    return _lblTitle;
}

- (AccountView *)accountView
{
    if (!_accountView) {
        _accountView = [[AccountView alloc] initWithFrame:CGRectMake(LoginVC_X, 0, LoginVC_W, 76.f) andAreaCode:@"86" andMobile:@""];
        _accountView.textColor = [UIColor blackColor];
    }
    return _accountView;
}

- (EPButton *)btnNext
{
    if (!_btnNext) {
        _btnNext = [[EPButton alloc] initWithFrame:CGRectMake(LoginVC_X, 0, LoginVC_W, Cell_H)];
        [_btnNext addTarget:self action:@selector(btnNextStepClick:) forControlEvents:UIControlEventTouchUpInside];
        [_btnNext setTitle:@"下一步" forState:UIControlStateNormal];
        [_btnNext buttonWithStyle:EPBasicButton_btn2];
        _btnNext.enabled = NO;
    }
    return _btnNext;
}

- (UIView *)protocolView
{
    if (!_protocolView) {
        _protocolView = [[UIView alloc] initWithFrame:CGRectMake(LoginVC_X, 0, LoginVC_W, 60.f)];
        
        UILabel *lblMsg = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, LoginVC_W, 20.f)];
        lblMsg.textColor = [UIColor blackColor];
        lblMsg.font = SystemFont(12.f);
        lblMsg.text = @"点击下一步即表示您同意协议：";
        if ((ForBindingMobileTypeFromWX == self.type) || (ForBindingMobileTypeFromFB == self.type)) {
            lblMsg.text = @"点击绑定即表示您同意协议：";
        }
        lblMsg.textAlignment = NSTextAlignmentCenter;
        [_protocolView addSubview:lblMsg];
        
        [_protocolView addSubview:self.btnProtocol];
        self.btnProtocol.top = lblMsg.bottom;
    }
    return _protocolView;
}

- (UIButton *)btnProtocol
{
    if (!_btnProtocol)
    {
        _btnProtocol = [[UIButton alloc] initWithFrame:CGRectMake(LoginVC_W/4, 0, LoginVC_W/2, 40.f)];
        [_btnProtocol setTitle:@"《用户使用协议》" forState:UIControlStateNormal];
        [_btnProtocol setTitleColor:[EPColor epColor_Blue] forState:UIControlStateNormal];
        _btnProtocol.titleLabel.font = SystemFont(12.f);
        [_btnProtocol addTarget:self action:@selector(btnProtocolClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnProtocol;
}

- (UILabel *)lblNote
{
    if (!_lblNote) {
        _lblNote = [[UILabel alloc] initWithFrame:CGRectMake(LoginVC_X, 0, LoginVC_W, 17.f)];
        _lblNote.font = SystemFont(12.f);
        _lblNote.textColor = [EPColor epColor_TitleBlack];
        _lblNote.text = @"更换手机号说明";
    }
    return _lblNote;
}

- (UILabel *)lblMobile
{
    if (!_lblMobile) {
        _lblMobile = [[UILabel alloc] initWithFrame:CGRectMake(LoginVC_X, 0, LoginVC_W, 17.f)];
        _lblMobile.font = SystemFont(12.f);
        _lblMobile.textColor = [EPColor epColor_TitleBlack];
    }
    return _lblMobile;
}

#pragma mark - 事件
- (void)btnNextStepClick:(id)sender
{
    self.strMobile = [self.accountView getAccount];
    self.strAreaCode = [self.accountView getAreaCode];
    
    // 发送短信验证码请求
    [self showProcessingHud:@"..."];
    
    if (ForRegisterType == self.type) {
        [(AccountSysVM *)self.baseVM registerSmsCodeRequestWithAreaCode:self.strAreaCode andMobile:self.strMobile andSource:@"phone"];
    }
    else if (ForForgotType == self.type) {
        [(AccountSysVM *)self.baseVM retrievePwdSmsCodeRequestWithAreaCode:self.strAreaCode andMobile:self.strMobile];
    }
    else if (ForModifyMobileType == self.type) {
        [(AccountSysVM *)self.baseVM modifyMobileSmsCodeRequestWithAreaCode:self.strAreaCode andMobile:self.strMobile];
    }
    else if (ForBindingMobileTypeFromWX == self.type) {
        [(AccountSysVM *)self.baseVM checkPhoneBindForRegisterWithAreaCode:self.strAreaCode andMobile:self.strMobile andSource:@"weixin"];
    }
    else if (ForBindingMobileTypeFromFB == self.type) {
        [(AccountSysVM *)self.baseVM checkPhoneBindForRegisterWithAreaCode:self.strAreaCode andMobile:self.strMobile andSource:@"facebook"];
    }
}

- (void)getSMSWithThirdParty
{
    self.strMobile = [self.accountView getAccount];
    self.strAreaCode = [self.accountView getAreaCode];
    
    if (ForBindingMobileTypeFromWX == self.type) {
        [(AccountSysVM *)self.baseVM registerSmsCodeRequestWithAreaCode:self.strAreaCode andMobile:self.strMobile andSource:@"weixin"];
    }
    else if (ForBindingMobileTypeFromFB == self.type) {
        [(AccountSysVM *)self.baseVM registerSmsCodeRequestWithAreaCode:self.strAreaCode andMobile:self.strMobile andSource:@"facebook"];
    }
}

#pragma mark - 用户使用协议
- (void)btnProtocolClick:(id)sender
{
    [MobClick event:@"click_ios_PersonalCenterVC_Service_Agreement"];
    BaseWebVC *vc = [[BaseWebVC alloc] init];
    vc.url = [NSString stringWithFormat:@"%@%@", UserSaveInfo.h5Url, ServiceAgreementUrl];
    vc.titleName = @"《用户使用协议》";
    [self.navigationController pushViewController:vc animated:YES];
}

@end
